package GUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.LayoutStyle.ComponentPlacement;
import controllers.DBConnection;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import java.awt.Color;

/**
 * Creating the Worker GUI
 *
 */
public class WorkerGUI extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	String s1,s2,s3,s4,s5;
	ArrayList<String> names=new ArrayList<String>();
	String str[];

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					WorkerGUI frame = new WorkerGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the Worker GUI frame.
	 */
	public WorkerGUI() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 1000, 600);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JButton btnNewButton = new JButton("Archive Managing");
		btnNewButton.setBounds(329, 200, 219, 60);
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Archive_Managing frame = new Archive_Managing();
				frame.setVisible(true);
				dispose();
			}
		});
		
		JLabel lblWelcomeToSgi = new JLabel("Welcome to SGi System");
		lblWelcomeToSgi.setForeground(Color.RED);
		lblWelcomeToSgi.setBackground(Color.RED);
		lblWelcomeToSgi.setBounds(175, 11, 430, 42);
		lblWelcomeToSgi.setFont(new Font("Tahoma", Font.BOLD, 32));
		
		JButton btnUpdatePrices = new JButton("Update Prices");
		btnUpdatePrices.setBounds(27, 93, 188, 69);
		btnUpdatePrices.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {				//********************//
				try{
				btnUpdatePricesActionPerformed(e);
				dispose();
				}catch(IOException | SQLException e1){
					e1.printStackTrace();
				}
			}
		});
		
		JButton btnReports = new JButton("Reports");
		btnReports.setBounds(27, 490, 188, 60);
		
		JButton btnUpdateCustomers = new JButton("Update Customers");
		btnUpdateCustomers.setBounds(329, 490, 219, 60);
		btnUpdateCustomers.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				UpdateCustomer frame = new UpdateCustomer();
				frame.setVisible(true);
				dispose();
			}
		});
		
		JButton btnComplaints = new JButton(" Read Complaints");
		btnComplaints.setBounds(328, 300, 220, 60);
		btnComplaints.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ReadComplaints frame = new ReadComplaints();
				frame.setVisible(true);
				dispose();
			}
		});
		
		JButton btnStatistics = new JButton("Statistics");			//*****************btnStatistics*************//
		btnStatistics.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try{
					btnStatisticsActionPerformed(e);
					dispose();
					}catch(IOException | SQLException e1){
						e1.printStackTrace();
					}
			}
		});
		
		
		
		btnStatistics.setBounds(27, 200, 188, 60);
		
		JButton btnMassages = new JButton("Messages");
		btnMassages.setBounds(27, 300, 188, 60);
		btnMassages.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});
		
		JLabel lblWorker = new JLabel("");
		lblWorker.setBounds(786, 372, 188, 178);
		lblWorker.setIcon(new ImageIcon("images//worker.jpg"));
		
		JButton btnSubscribtion = new JButton("Subscribtion");
		btnSubscribtion.setBounds(27, 393, 188, 60);
		btnSubscribtion.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SubscribeCustomer frame = new SubscribeCustomer();
				frame.setVisible(true);
				dispose();
			}
		});
		
		JButton btnRequestUpdatePrices = new JButton("Request Update Prices");  //***********************//
		btnRequestUpdatePrices.setBounds(329, 393, 219, 60);
		btnRequestUpdatePrices.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				RequestUpdatePricesUI frame = new RequestUpdatePricesUI();
					frame.setVisible(true);
					dispose();
			}
		});
		contentPane.setLayout(null);
		contentPane.add(lblWelcomeToSgi);
		contentPane.add(btnMassages);
		contentPane.add(btnSubscribtion);
		contentPane.add(btnRequestUpdatePrices);
		contentPane.add(btnStatistics);
		contentPane.add(btnComplaints);
		contentPane.add(btnUpdatePrices);
		contentPane.add(btnUpdateCustomers);
		contentPane.add(btnNewButton);
		contentPane.add(btnReports);
		contentPane.add(lblWorker);
		
		JLabel lblWorker_1 = new JLabel("Worker");
		lblWorker_1.setFont(new Font("Trebuchet MS", Font.BOLD, 14));
		lblWorker_1.setBounds(850, 346, 108, 14);
		contentPane.add(lblWorker_1);
		lblWorker_1.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(new ImageIcon("images\\sate.jpg"));
		lblNewLabel.setBounds(0, 0, 984, 561);
		contentPane.add(lblNewLabel);
		lblNewLabel.setLayout(null);
	}
	/**
	 * creating data base connection
	 * getting details from DB
	 * @param e
	 * @throws IOException
	 * @throws SQLException
	 */
	
	public void btnUpdatePricesActionPerformed(ActionEvent e) throws IOException, SQLException {
		
		String[] str={"101","102","103","104","105"};
		double d1,d2,d3,d4,d5;

		
		DBConnection conn=new DBConnection();
		
		java.sql.PreparedStatement pr= conn.getDBConnection().prepareStatement("SELECT * " + "FROM sgi_system.prices " + "WHERE ID ='" + str[0]+"';");
		ResultSet rs=pr.executeQuery();	
		if(!rs.next()){
			rs.close();
			pr.close();				
			JOptionPane.showMessageDialog(null, "ERROR");
		}
		else{
			d1=rs.getDouble("newPrice");
			this.s1=String.valueOf(d1);
			rs.close();
			pr.close();				
		}
		
		java.sql.PreparedStatement pr1= conn.getDBConnection().prepareStatement("SELECT * " + "FROM sgi_system.prices " + "WHERE ID ='" + str[1]+"';");
		ResultSet rs1=pr1.executeQuery();	
		if(!rs1.next()){
			rs1.close();
			pr1.close();				
		}
		else{
			d2=rs1.getDouble("newPrice");
			this.s2=String.valueOf(d2);
			rs1.close();
			pr1.close();				
		}
		
		java.sql.PreparedStatement pr11= conn.getDBConnection().prepareStatement("SELECT * " + "FROM sgi_system.prices " + "WHERE ID ='" + str[2]+"';");
		ResultSet rs11=pr11.executeQuery();	
		if(!rs11.next()){
			rs11.close();
			pr11.close();				
		}
		else{
			d3=rs11.getDouble("newPrice");
			this.s3=String.valueOf(d3);
			rs11.close();
			pr11.close();				
		}

		java.sql.PreparedStatement pr111= conn.getDBConnection().prepareStatement("SELECT * " + "FROM sgi_system.prices " + "WHERE ID ='" + str[3]+"';");
		ResultSet rs111=pr111.executeQuery();	
		if(!rs111.next()){
			rs111.close();
			pr111.close();				
		}
		else{
			d4=rs111.getDouble("newPrice");
			this.s4=String.valueOf(d4);
			rs111.close();
			pr111.close();				
		}

		java.sql.PreparedStatement pr1111= conn.getDBConnection().prepareStatement("SELECT * " + "FROM sgi_system.prices " + "WHERE ID ='" + str[4]+"';");
		ResultSet rs1111=pr1111.executeQuery();	
		if(!rs1111.next()){
			rs1111.close();
			pr1111.close();				
		}
		else{
			d5=rs1111.getDouble("newPrice");
			this.s5=String.valueOf(d5);
			rs1111.close();
			pr1111.close();				
		}
	
		this.names.add(s1);
		this.names.add(s2);
		this.names.add(s3);
		this.names.add(s4);
		this.names.add(s5);
		
		UpdatePricesUI frame = new UpdatePricesUI(names);
		frame.setVisible(true);
		
	}

	
	public void btnStatisticsActionPerformed(ActionEvent e) throws IOException, SQLException {
		
		int t1,t2,t3,t4,t5;
		ArrayList<String> arr=new ArrayList<String>();
		
		DBConnection conn=new DBConnection();
		
		java.sql.PreparedStatement pr= conn.getDBConnection().prepareStatement("SELECT *  FROM sgi_system.statistic WHERE reportID = ?");
		pr.setString(1,"2014-05-12 12:15:48");
		ResultSet rs=pr.executeQuery();	
		if(!rs.next()){
			rs.close();
			pr.close();				
			JOptionPane.showMessageDialog(null, "ERROR");
		}
		else{
			
			arr.add(String.valueOf(rs.getInt("complaints")));
			arr.add(String.valueOf(rs.getInt("totalPic")));
			arr.add(String.valueOf(rs.getInt("purchasesPic")));
			arr.add(String.valueOf(rs.getInt("layerPackage")));
			arr.add(String.valueOf(rs.getInt("picPackage")));
			rs.close();
			pr.close();
									

			BuildMonthlyReportUI frame = new BuildMonthlyReportUI(arr);
			frame.setVisible(true);
		}
	

	}
	
}
	

